package com.ihr360.dictionary.code.nation;


import com.ihr360.commons.model.IHierarchyEntity;
import com.ihr360.commons.model.dto.BaseCode;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/** 行政区划 */
@Entity
@Cacheable
@Cache(region = "cnb.base", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Division extends BaseCode<Integer> implements IHierarchyEntity<Division, Integer> {

    private static final long serialVersionUID = -8303016071363886718L;

    /** 国家 */
    @ManyToOne(fetch = FetchType.LAZY)
    private Country country;

    /** 简称 */
    @Column(length = 30)
    private String shortName;

    /** 首字母 */
    @Column(length = 1)
    private String initial;

    /** 名称首字母 */
    @Column(length = 20)
    private String fullInitial;

    /** 拼音 */
    @Column(length = 100)
    private String pinyin;

    private boolean hot = false;

    /** 下级区划列表 */
    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
    @Cache(region = "cnb.base", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private List<Division> children = new ArrayList<>();

    /** 上级区划 */
    @ManyToOne(fetch = FetchType.EAGER)
    @Cache(region = "cnb.base", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Division parent;

    public Division() {
    }

    public Division(Integer id) {
        super(id);
    }

    @Override
    public Division getParent() {
        return parent;
    }

    public Division getTop() {
        Division belongDivision = this;
        while (null != belongDivision && null != belongDivision.getParent()) {
            belongDivision = belongDivision.getParent();
        }
        return belongDivision;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    @Override
    public void setParent(Division superDivision) {
        this.parent = superDivision;
    }

    public boolean hasSubDivision() {
        return !this.children.isEmpty();
    }

    @Override
    public List<Division> getChildren() {
        return children;
    }

    @Override
    public void setChildren(List<Division> subDivisions) {
        this.children = subDivisions;
    }

    public boolean addSubDivision(Division subDivision) {
        return this.children.add(subDivision);
    }

    public boolean removeSubDivision(Division subDivision) {
        return this.children.remove(subDivision);
    }

    public boolean addSubDivisions(Collection<? extends Division> subDivisions) {
        return this.children.addAll(subDivisions);
    }

    public boolean removeSubDivisions(Collection<? extends Division> subDivisions) {
        return this.children.removeAll(subDivisions);
    }

    public void clearSubDivisions() {
        this.children.clear();
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public String getInitial() {
        return initial;
    }

    public void setInitial(String initial) {
        this.initial = initial;
    }

    public boolean isHot() {
        return hot;
    }

    public void setHot(boolean hot) {
        this.hot = hot;
    }

    public String getFullInitial() {
        return fullInitial;
    }

    public void setFullInitial(String fullInitial) {
        this.fullInitial = fullInitial;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    // TODO
    @Override
    public String getIndexno() {
        return null;
    }
}